#include <iostream>
#include <vector>
#include <cstdlib> // For rand() and srand()
#include <ctime>   // For time()

#define pi 3.14159265
#define SHAPE_COUNT 10 // 定义数组中形状的数量

using namespace std;

class Shape
{
public:
    virtual void CalculateArea() = 0; // 纯虚函数，确保派生类必须实现此函数
    virtual ~Shape() {} // 虚析构函数，确保派生类对象被正确删除
};

class Circle : public Shape
{
    double r;
public:
    Circle(double radius) : r(radius) {}
    void CalculateArea() override
    {
        cout << "S(circle)=" << r * r * pi << endl;
    }
    ~Circle() override {} // 显式提供析构函数，虽然默认析构函数就足够
};

class Rectangle : public Shape
{
    double a, b;
public:
    Rectangle(double length, double width) : a(length), b(width) {}
    void CalculateArea() override
    {
        cout << "S(rectangle)=" << a * b << endl;
    }
    ~Rectangle() override {} // 显式提供析构函数
};

int main()
{
    srand(time(0)); // 初始化随机数种子
    vector<Shape*> shapes; // 创建一个指向Shape的指针数组

    // 随机添加Circle和Rectangle对象
    for (int i = 0; i < SHAPE_COUNT; ++i)
    {
        if (rand() % 2) // 如果随机数是奇数，创建Circle
        {
            double radius = rand() % 10 + 1; // 随机半径
            shapes.push_back(new Circle(radius));
        }
        else // 如果随机数是偶数，创建Rectangle
        {
            double length = rand() % 10 + 1; // 随机长度
            double width = rand() % 10 + 1;  // 随机宽度
            shapes.push_back(new Rectangle(length, width));
        }
    }

    // 计算每个形状的面积
    for (Shape* shape : shapes)
    {
        shape->CalculateArea();
    }

    // 清理分配的内存
    for (Shape* shape : shapes)
    {
        delete shape;
    }

    return 0;
}
